package com.qcby.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.dao.ClassesDao;
import com.qcby.dao.TeachersDao;
import com.qcby.entity.Classes;
import com.qcby.entity.Student;
import com.qcby.entity.Tc;
import com.qcby.entity.Teachers;
import com.qcby.entity.vo.ClassesVo;
import com.qcby.entity.vo.TeachersVo;
import com.qcby.service.ClassesService;
import com.qcby.util.ResultObject;
import com.qcby.util.SystemDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author haozeWang
 * @create 2021-08-23 17:46
 */
@Service
public class ClassesServiceImpl implements ClassesService {
    @Autowired
    private ClassesDao classesDao;
    /*查所有*/
    @Override
    public ResultObject selectAll(ClassesVo classesVo) {
        Page<Object> page = PageHelper.startPage(classesVo.getPage(), classesVo.getLimit());
        if(classesVo.getCname()!=null&&!classesVo.getCname().equals("")){
            classesVo.setCname("%"+classesVo.getCname()+"%");
        }else {
            classesVo.setCname(null);
        }
        List<ClassesVo> classes = classesDao.selectAll(classesVo);
        ResultObject resultObject = new ResultObject("",0,page.getTotal(),classes);
        return resultObject;
    }
    /*延迟查询老师对应的班级*/
    @Override
    public ResultObject selectById(Integer id) {
        List<ClassesVo> classesVos=classesDao.selectJoinById(id);
        /*将查到的全部老师放在一个容器里*/
        ClassesVo classesVo = new ClassesVo();
        ArrayList<Teachers> teachers = new ArrayList();

        for (ClassesVo classvo:classesVos) {
            teachers.add(classvo.getTeachers().get(0));
        }
        classesVo.setTeachers(teachers);
        ResultObject resultObject = new ResultObject("",0,1L, classesVo);
        return resultObject;
    }
    /*删除单个byid*/
    @Override
    public ResultObject delete(Integer id) {
        Classes classes = new Classes();
        classes.setId(id);
        classes.setAvailable(SystemDescription.CODECURDFAIL);
        int i = classesDao.updateByPrimaryKeySelective(classes);
        classesDao.deltc(id);
        String msg=i==0?SystemDescription.DELETEFAIL:SystemDescription.DELETEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
   /*添加班级*/
    @Override
    public ResultObject add(ClassesVo classesVo) {
        int i = classesDao.insertSelective(classesVo);
        int classid=classesVo.getId();
        List<Integer> sel=classesVo.getSelect();
        Tc tc = new Tc();
        tc.setClassid(classid);
        for (int j = 0; j <sel.size() ; j++) {
            tc.setTeacherid(sel.get(j));
            classesDao.inserttc(tc);
        }
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.ADDFAIL:SystemDescription.ADDOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }
    /*修改班级*/
    @Override
    public ResultObject update(ClassesVo classesVo) {
        int i = classesDao.updateByPrimaryKeySelective(classesVo);
        int classid=classesVo.getId();
        classesDao.deltc(classid);
        List<Integer> sel=classesVo.getSelect();
        Tc tc = new Tc();
        tc.setClassid(classid);
        for (int j = 0; j <sel.size() ; j++) {
            tc.setTeacherid(sel.get(j));
            classesDao.inserttc(tc);
        }
        String msg = i== SystemDescription.CODEFAIL ?SystemDescription.UPDATEFAIL:SystemDescription.UPDATEOK;
        ResultObject resultObject = new ResultObject(i, msg);
        return resultObject;
    }

    /*批量删除班级*/
    @Override
    public ResultObject deleteDisp(ArrayList<Integer> ids) {
        int flag=SystemDescription.CODECURDOK;
        for (Integer id:ids) {
            ResultObject deleteitem = delete(id);
             if(deleteitem.getCode()==SystemDescription.CODECURDFAIL){
                 flag=SystemDescription.CODECURDFAIL;
             }
        }
        String msg=flag==0?SystemDescription.DELETEDISPFAIL:SystemDescription.DELETEDISPOK;
        ResultObject resultObject = new ResultObject(flag,msg);
        return resultObject;
    }
}
